package com.zking.ssm.DYM.controller;

import com.zking.ssm.DYM.model.HotelMember;
import com.zking.ssm.DYM.service.IHotelMemberService;
import com.zking.ssm.util.JsonData;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author XXXXX
 * @Date 2021/01/25 8:49
 */
@Controller
@RequestMapping("/hotelMember")
public class HotelMenberController {
    @Autowired
    private IHotelMemberService hotelMemberService;
    private PageBean pageBean;

    @ModelAttribute
    public void init(Model model) {
        System.out.println("init");
        HotelMember hotelMember = new HotelMember();
        model.addAttribute("hotelMember", hotelMember);
    }

    //添加数据
    //添加数据
    @RequestMapping("/toAddHotelMember")
    public String toAddHotelMember(Model model) {
        System.out.println("toAddHotelMember");
        return "addHotelMember";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HotelMember hotelMember) {
        System.out.println("add");
        hotelMemberService.insert(hotelMember);
        return "redirect:toAddHotelMember";
    }

    //修改数据
    //① 更新酒店房间，先通过ID找到酒店房间
    @RequestMapping("/updateHotelMember/{id}")
    public String updateHotelMember(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("updateHotelMember", hotelMemberService.selectByPrimaryKey(id));
        return "updateHotelMember";
    }

    //② 然后修改，点击更新提交数据给后端
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(HotelMember hotelMember) {
        hotelMemberService.updateByPrimaryKey(hotelMember);
        return "redirect:updateHotelMember";
    }

    //删除酒店房间
    @RequestMapping(value = "/deleteHotelMemberById")
    @ResponseBody
    public String deleteHotelMemberById(Integer id) {
        hotelMemberService.deleteByPrimaryKey(id);
        System.out.println(id);
        return "redirect:HotelMember";
//        return "删除成功";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonData list(HotelMember hotelMember, PageBean pageBean) {
        System.out.println("list");
        List<HotelMember> hotelMemberList = hotelMemberService.list(hotelMember, pageBean);
//      System.out.println("集合:"+hotelRoomList);
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setPage(pageBean.getPage());
        jsonData.setRows(pageBean.getRows());
        jsonData.setTotal(pageBean.getTotal());
        jsonData.setMessage("查询成功");
        jsonData.setResult(hotelMemberList);
        return jsonData;
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public JsonData get(HotelMember hotelMember) {
        HotelMember h = hotelMemberService.selectByPrimaryKey(hotelMember.getId());
        JsonData jsonData = new JsonData();
        jsonData.setCode(0);
        jsonData.setMessage("查询成功");
        jsonData.setResult(h);
        return jsonData;
    }
}
